package com.hc.webservice;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class ServiceHandler {
	static String response = null;
//	public final static int GET = 1;
	public final static int POST = 2;
	public final static int UPLOAD = 3;

	public ServiceHandler() {

	}

	//
	// public String makeServiceCall(String url, int method, List<NameValuePair>
	// pamram) {
	// return this.makeServiceCall(url, method, null);
	// }

	@SuppressWarnings("deprecation")
	public String makeServiceCall(String url, int method,
			List<NameValuePair> params) {
		try {
			// http client
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpEntity httpEntity = null;
			HttpResponse httpResponse = null;

			// Checking http request method type
			if (method == POST) {
				HttpPost httpPost = new HttpPost(url);
				// adding post params
				if (params != null) {

					// MultipartEntity entity = new
					// MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
					httpPost.setHeader("Content-Type",
			                    "application/x-www-form-urlencoded;charset=UTF-8");
					httpPost.setEntity(new UrlEncodedFormEntity(params));
				}

				httpResponse = httpClient.execute(httpPost);

			} else if (method == UPLOAD) {
				HttpPost httpPost = new HttpPost(url);
				// adding post params
				if (params != null) {

					for (int index = 0; index < params.size(); index++) {
						if (params.get(index).getName()
								.equalsIgnoreCase("file_contents")) {
							// If the key equals to "image", we use FileBody to
							// transfer
							// the data
							try {
								MultipartEntity entity = new MultipartEntity(
										HttpMultipartMode.BROWSER_COMPATIBLE);
								entity.addPart(params.get(index).getName(),
										new FileBody(new File(params.get(index)
												.getValue())));
							} catch (Exception e) {
								e.printStackTrace();
							}

						} else {
							// Normal string data
							// entity.addPart(
							// nameValuePairs.get(index).getName(),
							// new
							// StringBody(nameValuePairs.get(index).getValue()));
						}
					}

					// MultipartEntity entity = new
					// MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

					httpPost.setEntity(new UrlEncodedFormEntity(params));
				}

				httpResponse = httpClient.execute(httpPost);

			}

//			else if (method == GET) {
//				// appending params to url
//				if (params != null) {
//					HttpGet get =new HttpGet(url);
//					get.setHeader("Authorization",
//		                    "Bearer pOuJzg0Y0FtJpO-Ugson2P_8dbmvaDiBZs7Z-v_2eIgGgUfFsiWS-TOIkNlquCQGHuu3OyeWCFVIB7xJjEp6nKg9iY1-gzRfjHtdvBk7MVemtm6Ksf5AmmKmuZleLR_-bNMorwUOqTs0BCjAomM6h2peaXA3LTJ_-V6CDpnKVmmfTf0DvVQYBsbtLG94sSAGUToxjhw9GQhkukkSLvMIYMRKt-R5M2walOq63_eJx17y7HQxlxjOffR41wq4sANewOFlIOuf2czn2QP_FuVg_2SwUpeQctMGYwSgLC7H2gUPFtWzSWB9AhG4Pfwfmy0cd4mxZrrPBglehg8_VpK0pzXpEDMYJ-mBtpZOkAaTXSr0f8aw8Y2CMCmLgRUtC5_JA9NtLhJ6TEBDeNco8Fj6ZfMyZEDxB6xg3MD0fgs7fkbqSUGj4bV54DbvrniSKQ9GwZM29tHXI1xzrvnTg0NIg1ul4kOSWiVeW88-PUlUTXA9kzKsE20CL_CrbOJUlaFhSYHMz-KAisKgtUynMe0lUg");
//					
////					params.add(new BasicNameValuePair("access_token",
////							"wH5kvIVmBDj7lofkPhqRu0nt_TT74n4gmFF8hUWKL1WGSYt4W4ZNy_GO1lyNaky8KPVPleuBK5_S1nXa37Z0IGinAifF_hYx4dvF2CKm2KHHTOnWNeSskmW8i_Nx3oCa1ATZO4b9jk6bxqa60LdtL-TlOOss7rw3fAaW0EHfiCy-gWJPrVHglUL4CELuhbMYgcwgeoe_9NgK3dzSKGdRnnC2IWVgTJIcEW5zb4DXnE79UvEmhy7E4Mil2hbU0FpFYsTtBZ7SrlsClKBf91v-Btv-vvNDQu9Y5C1or4qUAuiR97yEzZxAbAtTvOCKUJx94HFVvMMTyVcHSqbEJpuPC-eLZFhU9xqzQiG2jqEwhlqNp9SM46gZwnbkFp2xDReo5Bcb_jlqhYDLmpDOiGyfsC4XMKISWehUfYGuC9WAEi8n2EiMAEF91uczIbM0EldE5Au5uc0OC31PYICZyvMb9Vtqu0ADpSgcgfvPWi-7QmcQDOc2RuBRmA5uzT1pT019OHfoDuOfXPux9cgXelNXYg"));
////					params.add(new BasicNameValuePair("token_type",
////							"bearer"));
////					String paramString = URLEncodedUtils
////							.format(params, "utf-8");
////					url += "?" + paramString;
//				}
//				HttpGet httpGet = new HttpGet(url);
//
//				httpResponse = httpClient.execute(httpGet);
//
//			}
			httpEntity = httpResponse.getEntity();
			response = EntityUtils.toString(httpEntity);

		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return response;

	}
}
